Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML File Formatting

Status
Not open for further replies.

cmhunt

Programmer
Apr 17, 2001
119
GB
Hi

I'm currently delivering a series of XML files to clients. Files are all well formed and abide by schema. Clients, however, would like the file formatted for readability. I've told them that they can open the file in IE which'll make it readable but that want line feeds and tabs actually applied within to the files. Current situation is that there are no line feeds or tab characters within the file and on viewing in notepad, all information is on one line (as you'd expect really!). Please could someone give me a hand where to start in order to format these files? Would I read in node by node and do the formatting manually or is there a more automated way of doing this?

Thanks for your help.

Chris
 
They sound like they want to process the XML files line-by-line, as if they were flat files. I would attempt to educate them on the use of a XML parser, because if they *are* going to treat them as flat files, then they'll complain when you change any little thing about the file. Add a new element? It would be OK with an XML parser because their XPath statements would hide that from them until they're ready to use it. But with reading it as an ordinary file, it would blow up their import code. yuck.

How are you creating the file today?

What you need to do is add a CR-LF after each closing element in order to get it to format for humans. If they want indention too, that's tougher, as you have to maintain an indention level variable, and every start element, increment it, and for every end element, decrement it. After your CR-LF, you would insert that many tab characters.

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
Also are you sure there aren't CRs in it? NotePad doesn't interpret documents the same as WordPad or Word, try opening it in either of them and see how it looks.

But I agree w/ Chip, if your users want to modify the data, get them an XML parser, viewer or something better then NotePad.

-Rick

----------------------
 
Hi

Chiph - apologies for cross post.

File genuinely has no line breaks in it. The file is being created using PHP. The file creation works fine and we have had no problems with this. My PHP knowledge isn't that brilliant so I'd prefer not to amend something that's working. I was thinking that a program which parses the output files would be the safest solution.

The idea I was thinking was to parse the file node by node, counting each level and then adding a line feed and indenting to the counted level. Just seemed like a lot of work for something I thought could have been easier.

Thanks for your help guys.

Chris
 
here's an idea.

use a dataset to read it in and re-save it. the dataset.writexml applys all the formating (CR-LF and Tabs).

-Rick

----------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top