Reading Data from Xml

Reading data from XML

I don't think you can "legally" load only part of an XML file, since then it would be malformed (there would be a missing closing element somewhere).

Using LINQ-to-XML, you can do var doc = XDocument.Load("yourfilepath"). From there its just a matter of querying the data you want, say like this:

var authors = doc.Root.Elements().Select( x => x.Element("Author") );

HTH.

EDIT:

Okay, just to make this a better sample, try this (with @JWL_'s suggested improvement):

using System;
using System.Xml.Linq;

namespace ConsoleApplication1 {
class Program {
static void Main( string[] args ) {
XDocument doc = XDocument.Load( "XMLFile1.xml" );
var authors = doc.Descendants( "Author" );
foreach ( var author in authors ) {
Console.WriteLine( author.Value );
}
Console.ReadLine();
}
}
}

You will need to adjust the path in XDocument.Load() to point to your XML file, but the rest should work. Ask questions about which parts you don't understand.

How to read data from xml file in python

You need to iterate each TExportCarcass tag and then use find to access BodyNum

Ex:

from lxml import etree

doc = etree.parse('file.xml')
for elem in doc.findall('TExportCarcass'):
print(elem.find("BodyNum").text)

Output:

6168
6169

or

print([i.text for i in doc.findall('TExportCarcass/BodyNum')]) #-->['6168', '6169']

C# Read and Store Data from XML

Are you sure you don't want to use the built-in Properties.Settings for this?

Circuit local = Properties.Settings.Default.localCircuit;
Circuit remote = Properties.Settings.Default.remoteCircuit;

https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/using-application-settings-and-user-settings

Reading data from XML into an array

Here is a LINQ to XML Version:

string[] arr = XDocument.Load(@"C:\xxx.xml").Descendants("Name")
.Select(element => element.Value).ToArray();

This will give all the Name element from the document.

Reading data from XML using C#

What you're looking for is XmlElement.InnerText.

When you get the node using this:

XmlElement node = (XmlElement)xDoc.SelectSingleNode("/order/ordertexts/ordertext");

You still need to use this:

string neededText = node.InnerText;

to get the value of that node.

Suppose that you're writing the results in a console application. If you try to write the node variable, this way:

Console.WriteLine(node);

Since node is not a string, and it's an XmlElement object, the ToString method of XmlElement is going to be called, which returns the object name, hence your new XML had the result as System.Xml.XmlElement and not the desired text.

reading data using JAVA from XML files

One of the simplest ways to do it is to use a DOM (Document Object Model) parser. It will load your xml document in memory and turns it into a tree made of Nodes so that you can travers it being able to get the information of any node at any position. It is memory consumming and is generally less prefered than a SAX parser.

Here is an example:

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

public class DomParsing {

public static final String ECB_DATAS ="C:\\xml\\eurofxref-hist-90d.xml";

public static void main(String argv[]) {

try {

File fXmlFile = new File(ECB_DATAS);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);

doc.getDocumentElement().normalize();

System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

NodeList nList = doc.getElementsByTagName("Cube");

for (int temp = 0; temp < nList.getLength(); temp++) {

Node nNode = nList.item(temp);

System.out.println("\nCurrent Element :" + nNode.getNodeName());

if (nNode.getNodeType() == Node.ELEMENT_NODE) {

Element eElement = (Element) nNode;

System.out.println("currency : " + eElement.getAttribute("currency") + " and rate is " + eElement.getAttribute("rate"));

}
}
} catch (Exception e) {
e.printStackTrace();
}
}

}

Applied to your file produces the following result:

currency : BGN and rate is 1.9558

Current Element :Cube

currency : CZK and rate is 27.797

Current Element :Cube

currency : DKK and rate is 7.444

Why can't it read the xml data properly?

I would simplify your parsing approach.

Take advantage of the event-based parser and remove all .find() calls. Look at it this way: The parser presents to you all the elements in the XML, you only have to decide which ones you find interesting.

In this case, the interesting elements have a certain tag name ('seg') and they need to be in a section with the right language. It's easy to have a Boolean flag (say, is_correct_language) that is toggled based on the xml:lang attribute of the previous <tuv> element.

Since the start event is enough for checking attributes and text, we don't need the parser to notify us of end events at all:

import xml.etree.ElementTree as ET

def parse(word, translator, file, language_target='de'):
is_correct_language = False

for event, elem in ET.iterparse(file, events=('start',)):
if elem.tag == 'tuv':
xml_lang = elem.attrib.get('{http://www.w3.org/XML/1998/namespace}lang', '')
is_correct_language = xml_lang.startswith(language_target)
elif is_correct_language and elem.tag == 'seg':
yield elem.text

usage:

for segment in parse('', '', 'test.xml', 'de'):
print(segment)

Other notes:

  • I've used a generator function (yield instead of return) as those tend to be more versatile.
  • I don't think that using os.getcwd() is a good idea in the function, as this needlessly restricts the function's usefulness.

    By default Python will look in the current working directory anyway,
    so in the best case prefixing the filename with os.getcwd() is
    superfluous. In the worst case you want to parse a file from a
    different directory and your function would needlessly break the
    path.
  • "File exists" checks are useless. Just open the file (or call ET.iterparse() on it, same thing). Wrap the whole thing in try/except and catch the FileNotFoundError, if you want to handle this situation.


Related Topics



Leave a reply



Submit