I am using xstream to marshal / unmarshal between java object to / from xml, one question is, is there a right solution to solve my problem (using xstream or other advanced method, instead of pure java API).
The existing XML file can grow quite big (more than 200 mb, for example), and I like to append the new xml to this existing XML file, but without unmarshal the existing XML file first, simply append it to the end (before the root element).
Please advice, thanks.
You could load the first XML as org.w3c.dom.Document and import the second one as org.w3c.dom.Element:
Element nodeToImport = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse( secondXmlFile ).getDocumentElement();
dom.importNode( nodeToImport, true );
...
There is a similar example here, but with a new node around the root one: Add an element around root element of given XML file that is stored in org.w3c.dom.Document
Related
I'm using dom4j to parse XML files on line.
File file = new File("text.xml");
SAXReader reader = new SAXReader();
Document document = reader.read(file);
There is a syntax error in some XML files, which make the program throw SAXParseException: The prefix X for element X:A is not bound at line reader.read(file).
I know what the error is, and how to make the XML files right.
Just as what said in this article
But the problem is the XML file is uploaded by users, I can not change the file before parse it, and I can not ask the user to change the XML file.
So, is there a way to parse the xml file with a undefined prefix error exist?
You could parse it with a DOM parser that isn't Namespace aware, but it will probably open up more problem areas than what you already have. The correct behavior is of course to validate the uploaded files and reject those that isn't correct XML or in any other way violate the contracts in place.
You do have the contracts in place, right?
I was trying to merge with git, but it apparently caused a problem with the XML file making a project unavailable. I know nothing about XML. Here is an excerpt of my file:
<ItemGroup>
<Content Include="MetroFramework.txt" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
An XML documents must have a single root element. To solve your problem:
Remove all root elements except for one, or
Wrap all top-level elements in a single root element.
Until you ensure that your XML document has a single root element, your file will not be well-formed (and will not actually be XML). Also, if your document is intended to follow a schema, make sure the root element and its contents (recursively) is valid according to the schema (XSD, DTD, etc). For more on this, see well-formed vs invalid XML.
As Alex K points out, your XML document looks like it's intended to be a MSBuild Project file.
First let me say I am very new to Java. I've been trying to figure out how to append a chunk of XML to an existing XML file using Xstream.
Example XML:
<root>
<first>
<a>Some Value</a>
<b>Some Value B</b>
</first
<second>
<a>Another Value</a>
<b>Another Value B</b>
</second>
</root>
How would I go about appending the following using Xstream?
<third>
<a>More A</a>
<b>More B</b>
</third>
Have you followed the two minute tutorial of Xstream yet? This can be found here.
You should address some implementation choices first: which way to go with Xstream?
For example: is the XML document a large document or a small document (if small, you could use a DomDriver.If large, use a StaxDriver)?
Does your XML document uses namespaces? If so, be aware that not all Xstream parsers implement namespace awareness, see the Xstream faq.
More information can be found here to get you started.
Please provide us your SSCCE so that users can try out your code example.
See details on how to write a SSCCE here and here
Also include a small valid XML file in your SSCCE.
Is it feasible in Java using the SAX api to parse a list of XML fragments with no root element from a stream input?
I tried parsing such an XML but got a
org.xml.sax.SAXParseException: The markup in the document following the root element must be well-formed.
before even the endDocument event was fired.
I would like not to settle with obvious but clumsy solutions as "Pre-append a custom root element or Use buffered fragment parsing".
I am using the standard SAX API of Java 1.6. The SAX factory had setValidating(false) in case anyone wondered.
First, and most important of all, the content you are parsing is not an XML document.
From the XML Specification:
[Definition: There is exactly one element, called the root, or document element, no part of which appears in the content of any other element.]
Now, as to parsing this with SAX - in spite of what you said about clumsiness - I'd suggest the following approach:
Enumeration<InputStream> streams = Collections.enumeration(
Arrays.asList(new InputStream[] {
new ByteArrayInputStream("<root>".getBytes()),
yourXmlLikeStream,
new ByteArrayInputStream("</root>".getBytes()),
}));
SequenceInputStream seqStream = new SequenceInputStream(streams);
// Now pass the `seqStream` into the SAX parser.
Using the SequenceInputStream is a convenient way of concatenating multiple input streams into a single stream. They will be read in the order they are passed to the constructor (or in this case - returned by the Enumeration).
Pass it to your SAX parser, and you are done.
I have an xml trying to parse & read it, but dont know how many nodes the xml may contain? So I am trying to read the node & node values ?
How I get the same say:
<company>
<personNam>John</personName>
<emailId>abc#test.com</emaiId>
<department>Products</department>
(may have additionaly nodes & values for same)
</company>
Sorry forgot to add my code, using Dom:-
Document document = getDocumentBuilder().parse(new ByteArrayInputStream(myXML.getBytes("UTF-8")));
String xPathExp = "//company";
XPath xPath = getXPath();
NodeList nodeList = (NodeList)xPath.evaluate(xPathExp, document, XPathConstants.NODESET);
nodeListSize = nodeList.getLength();
System.out.println("#####nodeListSize"+nodeListSize);
for(int i=0;i<nodeListSize;i++){
element=(Element)nodeList.item(i);
m1XMLOutputResponse=element.getTextContent();
System.out.println("#####"+element.getTagName()+" "+element.getTextContent());
}
Consider using the JAXB library. It's really a painless way of mapping your XML to Java classes and back. The basic principle is that JAXB takes your XML Schemas (XSD) and generates corresponding Java classes for you. Then you just call marshall or unmarshall methods which populate your Java class with the contents of the XML, or generates the XML from your Java class.
The only drawback is, of course, that you'd need to know how to write the XML Schemas :)
Learn how to use XML DOM. Here is an example on how to use XML DOM to fetch node and node values.